package com.ruoyi.back.v2.service

import cn.hutool.core.bean.BeanUtil
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.ruoyi.app.common.*
import com.ruoyi.back.service.AppUserService
import com.ruoyi.back.v2.domain.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class SpreadManagerAppService {

    @Autowired
    private lateinit var promoteRelationshipService: PromoteRelationshipService

    @Autowired
    private lateinit var inviteDetailService: InviteDetailService

    @Autowired
    private lateinit var withdrawalsDetailService: WithdrawalsDetailService

    @Autowired
    private lateinit var employeeDetailService: EmployeeDetailService

    @Autowired
    private lateinit var appUserService: AppUserService


    /**
     * 推广赚钱页面数据
     */
    fun spreadEmployee(spreadEmployeeDto: AppSpreadEmployeeDto): Data<AppSpreadManagerVo> =
        Ok("获取数据成功", AppSpreadManagerVo().apply {
            employeeMoney = promoteRelationshipService.userPromoteRelationShip(id())?.employeeMoney ?: 0.0
            inviteUserNum = inviteDetailService.inviteUserNum(0).size
            withdrawMoney = withdrawalsDetailService.withdrawMoney()
            totalEmployeeMoney = employeeDetailService.totalEmployee(0)

            appSpreadEmployeeVo = AppSpreadEmployeeVo().apply {
                inviteUserNum = inviteDetailService.inviteUserNum(spreadEmployeeDto.type).size
                inviteDetailService.consumerTotalNumAndMoney(spreadEmployeeDto.type).let {
                    consumeNum = it.first
                    consumeMoney = it.second
                }
                totalEmployee = employeeDetailService.totalEmployee(spreadEmployeeDto.type)

                todayInviteUserNum = inviteDetailService.inviteUserNum(spreadEmployeeDto.type, true).size
                inviteDetailService.consumerTotalNumAndMoney(spreadEmployeeDto.type, true).let {
                    consumeNum = it.first
                    consumeMoney = it.second
                }
                totalEmployee = employeeDetailService.totalEmployee(spreadEmployeeDto.type, true)
            }
        })

    /**
     * 已邀请用户信息
     */
    fun haveInviteInfo(
        spreadEmployeeDto: AppSpreadEmployeeDto,
        pageSize: Int?,
        pageNum: Int?
    ): PageData<AppInviteUserVo> =
        inviteDetailService.inviteUserPage(spreadEmployeeDto.type, pageNum ?: 1, pageSize ?: 10, id())
            .let { page ->
                page.records.takeIf { it.isNotEmpty() }
                    ?.let { inviteDetails ->
                        Page<AppInviteUserVo>().apply {
                            BeanUtil.copyProperties(page, this)
                            records = inviteDetails.map {
                                AppInviteUserVo().apply {
                                    appUserService.getById(it.inviteUserId)?.let {
                                        username = it.username
                                        userId = it.userId
                                    }
                                    time = it.createTime?.format()

                                    spreadEmployeeDto.type.takeIf { it == 2 }
                                        ?.run {
                                            promoteRelationshipService.userPromoteRelationShip(it.inviteUserId)
                                                ?.let {
                                                    appUserService.getById(it.firstLevel)?.let { appUser ->
                                                        firstLevelUserName = appUser.username
                                                        firstUserId = appUser.userId
                                                    }
                                                }
                                        }
                                }
                            }
                        }.pageInfo()
                    } ?: Page<AppInviteUserVo>().apply { total = 0; records = emptyList() }.pageInfo()
            }


    /**
     * 已提现数据
     */
    fun haveWithdraw(pageSize: Int?, pageNum: Int?): Data<AppWithdrawInfoVo> =
        Ok("获取数据成功", AppWithdrawInfoVo().apply {
            totalWithdrawMoney = withdrawalsDetailService.withdrawMoney()
            withdrawDetails = withdrawalsDetailService.withdrawPage(pageNum ?: 1, pageSize ?: 10, id())
                .let { page ->
                    page.records.takeIf { it.isNotEmpty() }
                        ?.let {
                            Page<AppWithdrawVo>().apply {
                                BeanUtil.copyProperties(page, this)
                                records = it.map {
                                    AppWithdrawVo().apply {
                                        BeanUtil.copyProperties(it, this)
                                        createTime = it.createTime?.format()
                                    }
                                }
                            }.pageInfo()
                        } ?: Page<AppWithdrawVo>().apply { total = 0; records = emptyList() }.pageInfo()
                }
        })


    /**
     * 累计收益
     */
    fun employeeData(pageSize: Int?, pageNum: Int?): Data<AppEmployeeInfoVo> =
        Ok("获取数据成功", AppEmployeeInfoVo().apply {
            totalEmployeeMoney = employeeDetailService.totalEmployee(null)
            employeeDetails = employeeDetailService.employeePage(pageNum ?: 1, pageSize ?: 10, id())
                .let { page ->
                    page.records.takeIf { it.isNotEmpty() }
                        ?.let {
                            Page<AppEmployeeVo>().apply {
                                BeanUtil.copyProperties(page, this)
                                records = it.map {
                                    AppEmployeeVo().apply {
                                        BeanUtil.copyProperties(it, this)
                                        createTime = it.createTime?.format()
                                    }
                                }
                            }.pageInfo()
                        } ?: Page<AppEmployeeVo>().apply { total = 0; records = emptyList() }.pageInfo()
                }
        })
}